concat         = require('gulp-concat')
filter         = require('gulp-filter')
gulp           = require('gulp')
mainBowerFiles = require('main-bower-files')
ngAnnotate     = require('gulp-ng-annotate')
order          = require 'gulp-order'
rename         = require('gulp-rename')
uglify         = require('gulp-uglify')
rev            = require 'gulp-rev'

gulp.task 'bower', ->
    jsFilter   = filter('*.js')
    cssFilter  = filter('*.css')
    fontFilter = filter(['*.eot', '*.svg', '*.woff', '*.ttf'])
    gulp.src(mainBowerFiles())
        .pipe(jsFilter)
        .pipe(order(['ui.js', '*']))
        .pipe(concat('vendor.js'))
        .pipe(jsFilter.restore())

        .pipe(cssFilter)
        .pipe(concat('vendor.css'))
        .pipe(cssFilter.restore())

        .pipe(fontFilter)
        .pipe(rename((path) -> # 对于字体，放到fonts目录下
            path.dirname = '/fonts'
        ))
        .pipe(fontFilter.restore())

        .pipe(gulp.dest('./www'))

gulp.task 'bower-optimize', ->
    jsFilter   = filter('*.js')
    cssFilter  = filter('*.css')
    fontFilter = filter(['*.eot', '*.svg', '*.woff', '*.ttf'])
    gulp.src(mainBowerFiles())
        .pipe(jsFilter)
        .pipe(order(['ionic.js', 'angular.js', 'ionic.angular.js', '*']))
        .pipe(concat('vendor.js'))
        .pipe(ngAnnotate()) # 对于非angular文件，这个调用会降低速度
        .pipe(uglify())
        .pipe rev()
        .pipe(jsFilter.restore())

        .pipe(fontFilter)
        .pipe(rename((path) -> # 对于字体，放到fonts目录下
            path.dirname = '/fonts'
        ))
        .pipe(fontFilter.restore())

        .pipe(gulp.dest('./www'))
        .pipe rev.manifest()
        .pipe(gulp.dest('rev/vendor_js'))
